home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / interp / perl5.005.tar.gz / perl5.005.tar / perl5.005 / pod / perlxs.pod < prev    next >
Text File  |  1998-07-22  |  46KB  |  1,349 lines

  1. =head1 NAME
  2.  
  3. perlxs - XS language reference manual
  4.  
  5. =head1 DESCRIPTION
  6.  
  7. =head2 Introduction
  8.  
  9. XS is a language used to create an extension interface
  10. between Perl and some C library which one wishes to use with
  11. Perl.  The XS interface is combined with the library to
  12. create a new library which can be linked to Perl.  An B<XSUB>
  13. is a function in the XS language and is the core component
  14. of the Perl application interface.
  15.  
  16. The XS compiler is called B<xsubpp>.  This compiler will embed
  17. the constructs necessary to let an XSUB, which is really a C
  18. function in disguise, manipulate Perl values and creates the
  19. glue necessary to let Perl access the XSUB.  The compiler
  20. uses B<typemaps> to determine how to map C function parameters
  21. and variables to Perl values.  The default typemap handles
  22. many common C types.  A supplement typemap must be created
  23. to handle special structures and types for the library being
  24. linked.
  25.  
  26. See L<perlxstut> for a tutorial on the whole extension creation process.
  27.  
  28. Note: For many extensions, Dave Beazley's SWIG system provides a
  29. significantly more convenient mechanism for creating the XS glue
  30. code. See L<http://www.cs.utah.edu/~beazley/SWIG> for more 
  31. information.
  32.  
  33. =head2 On The Road
  34.  
  35. Many of the examples which follow will concentrate on creating an interface
  36. between Perl and the ONC+ RPC bind library functions.  The rpcb_gettime()
  37. function is used to demonstrate many features of the XS language.  This
  38. function has two parameters; the first is an input parameter and the second
  39. is an output parameter.  The function also returns a status value.
  40.  
  41.     bool_t rpcb_gettime(const char *host, time_t *timep);
  42.  
  43. From C this function will be called with the following
  44. statements.
  45.  
  46.      #include <rpc/rpc.h>
  47.      bool_t status;
  48.      time_t timep;
  49.      status = rpcb_gettime( "localhost", &timep );
  50.  
  51. If an XSUB is created to offer a direct translation between this function
  52. and Perl, then this XSUB will be used from Perl with the following code.
  53. The $status and $timep variables will contain the output of the function.
  54.  
  55.      use RPC;
  56.      $status = rpcb_gettime( "localhost", $timep );
  57.  
  58. The following XS file shows an XS subroutine, or XSUB, which
  59. demonstrates one possible interface to the rpcb_gettime()
  60. function.  This XSUB represents a direct translation between
  61. C and Perl and so preserves the interface even from Perl.
  62. This XSUB will be invoked from Perl with the usage shown
  63. above.  Note that the first three #include statements, for
  64. C<EXTERN.h>, C<perl.h>, and C<XSUB.h>, will always be present at the
  65. beginning of an XS file.  This approach and others will be
  66. expanded later in this document.
  67.  
  68.      #include "EXTERN.h"
  69.      #include "perl.h"
  70.      #include "XSUB.h"
  71.      #include <rpc/rpc.h>
  72.  
  73.      MODULE = RPC  PACKAGE = RPC
  74.  
  75.      bool_t
  76.      rpcb_gettime(host,timep)
  77.           char *host
  78.           time_t &timep
  79.           OUTPUT:
  80.           timep
  81.  
  82. Any extension to Perl, including those containing XSUBs,
  83. should have a Perl module to serve as the bootstrap which
  84. pulls the extension into Perl.  This module will export the
  85. extension's functions and variables to the Perl program and
  86. will cause the extension's XSUBs to be linked into Perl.
  87. The following module will be used for most of the examples
  88. in this document and should be used from Perl with the C<use>
  89. command as shown earlier.  Perl modules are explained in
  90. more detail later in this document.
  91.  
  92.      package RPC;
  93.  
  94.      require Exporter;
  95.      require DynaLoader;
  96.      @ISA = qw(Exporter DynaLoader);
  97.      @EXPORT = qw( rpcb_gettime );
  98.  
  99.      bootstrap RPC;
  100.      1;
  101.  
  102. Throughout this document a variety of interfaces to the rpcb_gettime()
  103. XSUB will be explored.  The XSUBs will take their parameters in different
  104. orders or will take different numbers of parameters.  In each case the
  105. XSUB is an abstraction between Perl and the real C rpcb_gettime()
  106. function, and the XSUB must always ensure that the real rpcb_gettime()
  107. function is called with the correct parameters.  This abstraction will
  108. allow the programmer to create a more Perl-like interface to the C
  109. function.
  110.  
  111. =head2 The Anatomy of an XSUB
  112.  
  113. The following XSUB allows a Perl program to access a C library function
  114. called sin().  The XSUB will imitate the C function which takes a single
  115. argument and returns a single value.
  116.  
  117.      double
  118.      sin(x)
  119.        double x
  120.  
  121. When using C pointers the indirection operator C<*> should be considered
  122. part of the type and the address operator C<&> should be considered part of
  123. the variable, as is demonstrated in the rpcb_gettime() function above.  See
  124. the section on typemaps for more about handling qualifiers and unary
  125. operators in C types.
  126.  
  127. The function name and the return type must be placed on
  128. separate lines.
  129.  
  130.   INCORRECT                        CORRECT
  131.  
  132.   double sin(x)                    double
  133.     double x                       sin(x)
  134.                      double x
  135.  
  136. The function body may be indented or left-adjusted.  The following example
  137. shows a function with its body left-adjusted.  Most examples in this
  138. document will indent the body.
  139.  
  140.   CORRECT
  141.  
  142.   double
  143.   sin(x)
  144.   double x
  145.  
  146. =head2 The Argument Stack
  147.  
  148. The argument stack is used to store the values which are
  149. sent as parameters to the XSUB and to store the XSUB's
  150. return value.  In reality all Perl functions keep their
  151. values on this stack at the same time, each limited to its
  152. own range of positions on the stack.  In this document the
  153. first position on that stack which belongs to the active
  154. function will be referred to as position 0 for that function.
  155.  
  156. XSUBs refer to their stack arguments with the macro B<ST(x)>, where I<x>
  157. refers to a position in this XSUB's part of the stack.  Position 0 for that
  158. function would be known to the XSUB as ST(0).  The XSUB's incoming
  159. parameters and outgoing return values always begin at ST(0).  For many
  160. simple cases the B<xsubpp> compiler will generate the code necessary to
  161. handle the argument stack by embedding code fragments found in the
  162. typemaps.  In more complex cases the programmer must supply the code.
  163.  
  164. =head2 The RETVAL Variable
  165.  
  166. The RETVAL variable is a magic variable which always matches
  167. the return type of the C library function.  The B<xsubpp> compiler will
  168. supply this variable in each XSUB and by default will use it to hold the
  169. return value of the C library function being called.  In simple cases the
  170. value of RETVAL will be placed in ST(0) of the argument stack where it can
  171. be received by Perl as the return value of the XSUB.
  172.  
  173. If the XSUB has a return type of C<void> then the compiler will
  174. not supply a RETVAL variable for that function.  When using
  175. the PPCODE: directive the RETVAL variable is not needed, unless used
  176. explicitly.
  177.  
  178. If PPCODE: directive is not used, C<void> return value should be used
  179. only for subroutines which do not return a value, I<even if> CODE:
  180. directive is used which sets ST(0) explicitly.
  181.  
  182. Older versions of this document recommended to use C<void> return
  183. value in such cases. It was discovered that this could lead to
  184. segfaults in cases when XSUB was I<truely> C<void>. This practice is
  185. now deprecated, and may be not supported at some future version. Use
  186. the return value C<SV *> in such cases. (Currently C<xsubpp> contains
  187. some heuristic code which tries to disambiguate between "truely-void"
  188. and "old-practice-declared-as-void" functions. Hence your code is at
  189. mercy of this heuristics unless you use C<SV *> as return value.)
  190.  
  191. =head2 The MODULE Keyword
  192.  
  193. The MODULE keyword is used to start the XS code and to
  194. specify the package of the functions which are being
  195. defined.  All text preceding the first MODULE keyword is
  196. considered C code and is passed through to the output
  197. untouched.  Every XS module will have a bootstrap function
  198. which is used to hook the XSUBs into Perl.  The package name
  199. of this bootstrap function will match the value of the last
  200. MODULE statement in the XS source files.  The value of
  201. MODULE should always remain constant within the same XS
  202. file, though this is not required.
  203.  
  204. The following example will start the XS code and will place
  205. all functions in a package named RPC.
  206.  
  207.      MODULE = RPC
  208.  
  209. =head2 The PACKAGE Keyword
  210.  
  211. When functions within an XS source file must be separated into packages
  212. the PACKAGE keyword should be used.  This keyword is used with the MODULE
  213. keyword and must follow immediately after it when used.
  214.  
  215.      MODULE = RPC  PACKAGE = RPC
  216.  
  217.      [ XS code in package RPC ]
  218.  
  219.      MODULE = RPC  PACKAGE = RPCB
  220.  
  221.      [ XS code in package RPCB ]
  222.  
  223.      MODULE = RPC  PACKAGE = RPC
  224.  
  225.      [ XS code in package RPC ]
  226.  
  227. Although this keyword is optional and in some cases provides redundant
  228. information it should always be used.  This keyword will ensure that the
  229. XSUBs appear in the desired package.
  230.  
  231. =head2 The PREFIX Keyword
  232.  
  233. The PREFIX keyword designates prefixes which should be
  234. removed from the Perl function names.  If the C function is
  235. C<rpcb_gettime()> and the PREFIX value is C<rpcb_> then Perl will
  236. see this function as C<gettime()>.
  237.  
  238. This keyword should follow the PACKAGE keyword when used.
  239. If PACKAGE is not used then PREFIX should follow the MODULE
  240. keyword.
  241.  
  242.      MODULE = RPC  PREFIX = rpc_
  243.  
  244.      MODULE = RPC  PACKAGE = RPCB  PREFIX = rpcb_
  245.  
  246. =head2 The OUTPUT: Keyword
  247.  
  248. The OUTPUT: keyword indicates that certain function parameters should be
  249. updated (new values made visible to Perl) when the XSUB terminates or that
  250. certain values should be returned to the calling Perl function.  For
  251. simple functions, such as the sin() function above, the RETVAL variable is
  252. automatically designated as an output value.  In more complex functions
  253. the B<xsubpp> compiler will need help to determine which variables are output
  254. variables.
  255.  
  256. This keyword will normally be used to complement the CODE:  keyword.
  257. The RETVAL variable is not recognized as an output variable when the
  258. CODE: keyword is present.  The OUTPUT:  keyword is used in this
  259. situation to tell the compiler that RETVAL really is an output
  260. variable.
  261.  
  262. The OUTPUT: keyword can also be used to indicate that function parameters
  263. are output variables.  This may be necessary when a parameter has been
  264. modified within the function and the programmer would like the update to
  265. be seen by Perl.
  266.  
  267.      bool_t
  268.      rpcb_gettime(host,timep)
  269.           char *host
  270.           time_t &timep
  271.           OUTPUT:
  272.           timep
  273.  
  274. The OUTPUT: keyword will also allow an output parameter to
  275. be mapped to a matching piece of code rather than to a
  276. typemap.
  277.  
  278.      bool_t
  279.      rpcb_gettime(host,timep)
  280.           char *host
  281.           time_t &timep
  282.           OUTPUT:
  283.           timep sv_setnv(ST(1), (double)timep);
  284.  
  285. B<xsubpp> emits an automatic C<SvSETMAGIC()> for all parameters in the
  286. OUTPUT section of the XSUB, except RETVAL.  This is the usually desired
  287. behavior, as it takes care of properly invoking 'set' magic on output
  288. parameters (needed for hash or array element parameters that must be
  289. created if they didn't exist).  If for some reason, this behavior is
  290. not desired, the OUTPUT section may contain a C<SETMAGIC: DISABLE> line
  291. to disable it for the remainder of the parameters in the OUTPUT section.
  292. Likewise,  C<SETMAGIC: ENABLE> can be used to reenable it for the
  293. remainder of the OUTPUT section.  See L<perlguts> for more details
  294. about 'set' magic.
  295.  
  296. =head2 The CODE: Keyword
  297.  
  298. This keyword is used in more complicated XSUBs which require
  299. special handling for the C function.  The RETVAL variable is
  300. available but will not be returned unless it is specified
  301. under the OUTPUT: keyword.
  302.  
  303. The following XSUB is for a C function which requires special handling of
  304. its parameters.  The Perl usage is given first.
  305.  
  306.      $status = rpcb_gettime( "localhost", $timep );
  307.  
  308. The XSUB follows.
  309.  
  310.      bool_t
  311.      rpcb_gettime(host,timep)
  312.           char *host
  313.           time_t timep
  314.           CODE:
  315.                RETVAL = rpcb_gettime( host, &timep );
  316.           OUTPUT:
  317.           timep
  318.           RETVAL
  319.  
  320. =head2 The INIT: Keyword
  321.  
  322. The INIT: keyword allows initialization to be inserted into the XSUB before
  323. the compiler generates the call to the C function.  Unlike the CODE: keyword
  324. above, this keyword does not affect the way the compiler handles RETVAL.
  325.  
  326.     bool_t
  327.     rpcb_gettime(host,timep)
  328.           char *host
  329.           time_t &timep
  330.       INIT:
  331.       printf("# Host is %s\n", host );
  332.           OUTPUT:
  333.           timep
  334.  
  335. =head2 The NO_INIT Keyword
  336.  
  337. The NO_INIT keyword is used to indicate that a function
  338. parameter is being used only as an output value.  The B<xsubpp>
  339. compiler will normally generate code to read the values of
  340. all function parameters from the argument stack and assign
  341. them to C variables upon entry to the function.  NO_INIT
  342. will tell the compiler that some parameters will be used for
  343. output rather than for input and that they will be handled
  344. before the function terminates.
  345.  
  346. The following example shows a variation of the rpcb_gettime() function.
  347. This function uses the timep variable only as an output variable and does
  348. not care about its initial contents.
  349.  
  350.      bool_t
  351.      rpcb_gettime(host,timep)
  352.           char *host
  353.           time_t &timep = NO_INIT
  354.           OUTPUT:
  355.           timep
  356.  
  357. =head2 Initializing Function Parameters
  358.  
  359. Function parameters are normally initialized with their
  360. values from the argument stack.  The typemaps contain the
  361. code segments which are used to transfer the Perl values to
  362. the C parameters.  The programmer, however, is allowed to
  363. override the typemaps and supply alternate (or additional)
  364. initialization code.
  365.  
  366. The following code demonstrates how to supply initialization code for
  367. function parameters.  The initialization code is eval'd within double
  368. quotes by the compiler before it is added to the output so anything
  369. which should be interpreted literally [mainly C<$>, C<@>, or C<\\>]
  370. must be protected with backslashes.  The variables C<$var>, C<$arg>,
  371. and C<$type> can be used as in typemaps.
  372.  
  373.      bool_t
  374.      rpcb_gettime(host,timep)
  375.           char *host = (char *)SvPV($arg,PL_na);
  376.           time_t &timep = 0;
  377.           OUTPUT:
  378.           timep
  379.  
  380. This should not be used to supply default values for parameters.  One
  381. would normally use this when a function parameter must be processed by
  382. another library function before it can be used.  Default parameters are
  383. covered in the next section.
  384.  
  385. If the initialization begins with C<=>, then it is output on
  386. the same line where the input variable is declared.  If the
  387. initialization begins with C<;> or C<+>, then it is output after
  388. all of the input variables have been declared.  The C<=> and C<;>
  389. cases replace the initialization normally supplied from the typemap.
  390. For the C<+> case, the initialization from the typemap will preceed
  391. the initialization code included after the C<+>.  A global
  392. variable, C<%v>, is available for the truely rare case where
  393. information from one initialization is needed in another
  394. initialization.
  395.  
  396.      bool_t
  397.      rpcb_gettime(host,timep)
  398.           time_t &timep ; /*\$v{time}=@{[$v{time}=$arg]}*/
  399.           char *host + SvOK($v{time}) ? SvPV($arg,PL_na) : NULL;
  400.           OUTPUT:
  401.           timep
  402.  
  403. =head2 Default Parameter Values
  404.  
  405. Default values can be specified for function parameters by
  406. placing an assignment statement in the parameter list.  The
  407. default value may be a number or a string.  Defaults should
  408. always be used on the right-most parameters only.
  409.  
  410. To allow the XSUB for rpcb_gettime() to have a default host
  411. value the parameters to the XSUB could be rearranged.  The
  412. XSUB will then call the real rpcb_gettime() function with
  413. the parameters in the correct order.  Perl will call this
  414. XSUB with either of the following statements.
  415.  
  416.      $status = rpcb_gettime( $timep, $host );
  417.  
  418.      $status = rpcb_gettime( $timep );
  419.  
  420. The XSUB will look like the code  which  follows.   A  CODE:
  421. block  is used to call the real rpcb_gettime() function with
  422. the parameters in the correct order for that function.
  423.  
  424.      bool_t
  425.      rpcb_gettime(timep,host="localhost")
  426.           char *host
  427.           time_t timep = NO_INIT
  428.           CODE:
  429.                RETVAL = rpcb_gettime( host, &timep );
  430.           OUTPUT:
  431.           timep
  432.           RETVAL
  433.  
  434. =head2 The PREINIT: Keyword
  435.  
  436. The PREINIT: keyword allows extra variables to be declared before the
  437. typemaps are expanded.  If a variable is declared in a CODE: block then that
  438. variable will follow any typemap code.  This may result in a C syntax
  439. error.  To force the variable to be declared before the typemap code, place
  440. it into a PREINIT: block.  The PREINIT: keyword may be used one or more
  441. times within an XSUB.
  442.  
  443. The following examples are equivalent, but if the code is using complex
  444. typemaps then the first example is safer.
  445.  
  446.      bool_t
  447.      rpcb_gettime(timep)
  448.           time_t timep = NO_INIT
  449.       PREINIT:
  450.           char *host = "localhost";
  451.           CODE:
  452.       RETVAL = rpcb_gettime( host, &timep );
  453.           OUTPUT:
  454.           timep
  455.           RETVAL
  456.  
  457. A correct, but error-prone example.
  458.  
  459.      bool_t
  460.      rpcb_gettime(timep)
  461.           time_t timep = NO_INIT
  462.       CODE:
  463.           char *host = "localhost";
  464.       RETVAL = rpcb_gettime( host, &timep );
  465.           OUTPUT:
  466.           timep
  467.           RETVAL
  468.  
  469. =head2 The SCOPE: Keyword
  470.  
  471. The SCOPE: keyword allows scoping to be enabled for a particular XSUB. If
  472. enabled, the XSUB will invoke ENTER and LEAVE automatically.
  473.  
  474. To support potentially complex type mappings, if a typemap entry used
  475. by this XSUB contains a comment like C</*scope*/> then scoping will
  476. automatically be enabled for that XSUB.
  477.  
  478. To enable scoping:
  479.  
  480.     SCOPE: ENABLE
  481.  
  482. To disable scoping:
  483.  
  484.     SCOPE: DISABLE
  485.  
  486. =head2 The INPUT: Keyword
  487.  
  488. The XSUB's parameters are usually evaluated immediately after entering the
  489. XSUB.  The INPUT: keyword can be used to force those parameters to be
  490. evaluated a little later.  The INPUT: keyword can be used multiple times
  491. within an XSUB and can be used to list one or more input variables.  This
  492. keyword is used with the PREINIT: keyword.
  493.  
  494. The following example shows how the input parameter C<timep> can be
  495. evaluated late, after a PREINIT.
  496.  
  497.     bool_t
  498.     rpcb_gettime(host,timep)
  499.           char *host
  500.       PREINIT:
  501.       time_t tt;
  502.       INPUT:
  503.           time_t timep
  504.           CODE:
  505.                RETVAL = rpcb_gettime( host, &tt );
  506.            timep = tt;
  507.           OUTPUT:
  508.           timep
  509.           RETVAL
  510.  
  511. The next example shows each input parameter evaluated late.
  512.  
  513.     bool_t
  514.     rpcb_gettime(host,timep)
  515.       PREINIT:
  516.       time_t tt;
  517.       INPUT:
  518.           char *host
  519.       PREINIT:
  520.       char *h;
  521.       INPUT:
  522.           time_t timep
  523.           CODE:
  524.            h = host;
  525.            RETVAL = rpcb_gettime( h, &tt );
  526.            timep = tt;
  527.           OUTPUT:
  528.           timep
  529.           RETVAL
  530.  
  531. =head2 Variable-length Parameter Lists
  532.  
  533. XSUBs can have variable-length parameter lists by specifying an ellipsis
  534. C<(...)> in the parameter list.  This use of the ellipsis is similar to that
  535. found in ANSI C.  The programmer is able to determine the number of
  536. arguments passed to the XSUB by examining the C<items> variable which the
  537. B<xsubpp> compiler supplies for all XSUBs.  By using this mechanism one can
  538. create an XSUB which accepts a list of parameters of unknown length.
  539.  
  540. The I<host> parameter for the rpcb_gettime() XSUB can be
  541. optional so the ellipsis can be used to indicate that the
  542. XSUB will take a variable number of parameters.  Perl should
  543. be able to call this XSUB with either of the following statements.
  544.  
  545.      $status = rpcb_gettime( $timep, $host );
  546.  
  547.      $status = rpcb_gettime( $timep );
  548.  
  549. The XS code, with ellipsis, follows.
  550.  
  551.      bool_t
  552.      rpcb_gettime(timep, ...)
  553.           time_t timep = NO_INIT
  554.       PREINIT:
  555.           char *host = "localhost";
  556.           CODE:
  557.           if( items > 1 )
  558.                host = (char *)SvPV(ST(1), PL_na);
  559.           RETVAL = rpcb_gettime( host, &timep );
  560.           OUTPUT:
  561.           timep
  562.           RETVAL
  563.  
  564. =head2 The C_ARGS: Keyword
  565.  
  566. The C_ARGS: keyword allows creating of XSUBS which have different
  567. calling sequence from Perl than from C, without a need to write
  568. CODE: or CPPCODE: section.  The contents of the C_ARGS: paragraph is
  569. put as the argument to the called C function without any change.
  570.  
  571. For example, suppose that C function is declared as
  572.  
  573.     symbolic nth_derivative(int n, symbolic function, int flags);
  574.  
  575. and that the default flags are kept in a global C variable
  576. C<default_flags>.  Suppose that you want to create an interface which
  577. is called as
  578.  
  579.     $second_deriv = $function->nth_derivative(2);
  580.  
  581. To do this, declare the XSUB as
  582.  
  583.     symbolic
  584.     nth_derivative(function, n)
  585.     symbolic    function
  586.     int        n
  587.     C_ARGS:
  588.     n, function, default_flags
  589.  
  590. =head2 The PPCODE: Keyword
  591.  
  592. The PPCODE: keyword is an alternate form of the CODE: keyword and is used
  593. to tell the B<xsubpp> compiler that the programmer is supplying the code to
  594. control the argument stack for the XSUBs return values.  Occasionally one
  595. will want an XSUB to return a list of values rather than a single value.
  596. In these cases one must use PPCODE: and then explicitly push the list of
  597. values on the stack.  The PPCODE: and CODE:  keywords are not used
  598. together within the same XSUB.
  599.  
  600. The following XSUB will call the C rpcb_gettime() function
  601. and will return its two output values, timep and status, to
  602. Perl as a single list.
  603.  
  604.      void
  605.      rpcb_gettime(host)
  606.           char *host
  607.       PREINIT:
  608.           time_t  timep;
  609.           bool_t  status;
  610.           PPCODE:
  611.           status = rpcb_gettime( host, &timep );
  612.           EXTEND(SP, 2);
  613.           PUSHs(sv_2mortal(newSViv(status)));
  614.           PUSHs(sv_2mortal(newSViv(timep)));
  615.  
  616. Notice that the programmer must supply the C code necessary
  617. to have the real rpcb_gettime() function called and to have
  618. the return values properly placed on the argument stack.
  619.  
  620. The C<void> return type for this function tells the B<xsubpp> compiler that
  621. the RETVAL variable is not needed or used and that it should not be created.
  622. In most scenarios the void return type should be used with the PPCODE:
  623. directive.
  624.  
  625. The EXTEND() macro is used to make room on the argument
  626. stack for 2 return values.  The PPCODE: directive causes the
  627. B<xsubpp> compiler to create a stack pointer available as C<SP>, and it
  628. is this pointer which is being used in the EXTEND() macro.
  629. The values are then pushed onto the stack with the PUSHs()
  630. macro.
  631.  
  632. Now the rpcb_gettime() function can be used from Perl with
  633. the following statement.
  634.  
  635.      ($status, $timep) = rpcb_gettime("localhost");
  636.  
  637. When handling output parameters with a PPCODE section, be sure to handle
  638. 'set' magic properly.  See L<perlguts> for details about 'set' magic.
  639.  
  640. =head2 Returning Undef And Empty Lists
  641.  
  642. Occasionally the programmer will want to return simply
  643. C<undef> or an empty list if a function fails rather than a
  644. separate status value.  The rpcb_gettime() function offers
  645. just this situation.  If the function succeeds we would like
  646. to have it return the time and if it fails we would like to
  647. have undef returned.  In the following Perl code the value
  648. of $timep will either be undef or it will be a valid time.
  649.  
  650.      $timep = rpcb_gettime( "localhost" );
  651.  
  652. The following XSUB uses the C<SV *> return type as a mnemonic only,
  653. and uses a CODE: block to indicate to the compiler
  654. that the programmer has supplied all the necessary code.  The
  655. sv_newmortal() call will initialize the return value to undef, making that
  656. the default return value.
  657.  
  658.      SV *
  659.      rpcb_gettime(host)
  660.           char *  host
  661.       PREINIT:
  662.           time_t  timep;
  663.           bool_t x;
  664.           CODE:
  665.           ST(0) = sv_newmortal();
  666.           if( rpcb_gettime( host, &timep ) )
  667.                sv_setnv( ST(0), (double)timep);
  668.  
  669. The next example demonstrates how one would place an explicit undef in the
  670. return value, should the need arise.
  671.  
  672.      SV *
  673.      rpcb_gettime(host)
  674.           char *  host
  675.       PREINIT:
  676.           time_t  timep;
  677.           bool_t x;
  678.           CODE:
  679.           ST(0) = sv_newmortal();
  680.           if( rpcb_gettime( host, &timep ) ){
  681.                sv_setnv( ST(0), (double)timep);
  682.           }
  683.           else{
  684.                ST(0) = &PL_sv_undef;
  685.           }
  686.  
  687. To return an empty list one must use a PPCODE: block and
  688. then not push return values on the stack.
  689.  
  690.      void
  691.      rpcb_gettime(host)
  692.           char *host
  693.       PREINIT:
  694.           time_t  timep;
  695.           PPCODE:
  696.           if( rpcb_gettime( host, &timep ) )
  697.                PUSHs(sv_2mortal(newSViv(timep)));
  698.           else{
  699.           /* Nothing pushed on stack, so an empty */
  700.           /* list is implicitly returned. */
  701.           }
  702.  
  703. Some people may be inclined to include an explicit C<return> in the above
  704. XSUB, rather than letting control fall through to the end.  In those
  705. situations C<XSRETURN_EMPTY> should be used, instead.  This will ensure that
  706. the XSUB stack is properly adjusted.  Consult L<perlguts/"API LISTING"> for
  707. other C<XSRETURN> macros.
  708.  
  709. =head2 The REQUIRE: Keyword
  710.  
  711. The REQUIRE: keyword is used to indicate the minimum version of the
  712. B<xsubpp> compiler needed to compile the XS module.  An XS module which
  713. contains the following statement will compile with only B<xsubpp> version
  714. 1.922 or greater:
  715.  
  716.     REQUIRE: 1.922
  717.  
  718. =head2 The CLEANUP: Keyword
  719.  
  720. This keyword can be used when an XSUB requires special cleanup procedures
  721. before it terminates.  When the CLEANUP:  keyword is used it must follow
  722. any CODE:, PPCODE:, or OUTPUT: blocks which are present in the XSUB.  The
  723. code specified for the cleanup block will be added as the last statements
  724. in the XSUB.
  725.  
  726. =head2 The BOOT: Keyword
  727.  
  728. The BOOT: keyword is used to add code to the extension's bootstrap
  729. function.  The bootstrap function is generated by the B<xsubpp> compiler and
  730. normally holds the statements necessary to register any XSUBs with Perl.
  731. With the BOOT: keyword the programmer can tell the compiler to add extra
  732. statements to the bootstrap function.
  733.  
  734. This keyword may be used any time after the first MODULE keyword and should
  735. appear on a line by itself.  The first blank line after the keyword will
  736. terminate the code block.
  737.  
  738.      BOOT:
  739.      # The following message will be printed when the
  740.      # bootstrap function executes.
  741.      printf("Hello from the bootstrap!\n");
  742.  
  743. =head2 The VERSIONCHECK: Keyword
  744.  
  745. The VERSIONCHECK: keyword corresponds to B<xsubpp>'s C<-versioncheck> and
  746. C<-noversioncheck> options.  This keyword overrides the command line
  747. options.  Version checking is enabled by default.  When version checking is
  748. enabled the XS module will attempt to verify that its version matches the
  749. version of the PM module.
  750.  
  751. To enable version checking:
  752.  
  753.     VERSIONCHECK: ENABLE
  754.  
  755. To disable version checking:
  756.  
  757.     VERSIONCHECK: DISABLE
  758.  
  759. =head2 The PROTOTYPES: Keyword
  760.  
  761. The PROTOTYPES: keyword corresponds to B<xsubpp>'s C<-prototypes> and
  762. C<-noprototypes> options.  This keyword overrides the command line options.
  763. Prototypes are enabled by default.  When prototypes are enabled XSUBs will
  764. be given Perl prototypes.  This keyword may be used multiple times in an XS
  765. module to enable and disable prototypes for different parts of the module.
  766.  
  767. To enable prototypes:
  768.  
  769.     PROTOTYPES: ENABLE
  770.  
  771. To disable prototypes:
  772.  
  773.     PROTOTYPES: DISABLE
  774.  
  775. =head2 The PROTOTYPE: Keyword
  776.  
  777. This keyword is similar to the PROTOTYPES: keyword above but can be used to
  778. force B<xsubpp> to use a specific prototype for the XSUB.  This keyword
  779. overrides all other prototype options and keywords but affects only the
  780. current XSUB.  Consult L<perlsub/Prototypes> for information about Perl
  781. prototypes.
  782.  
  783.     bool_t
  784.     rpcb_gettime(timep, ...)
  785.           time_t timep = NO_INIT
  786.       PROTOTYPE: $;$
  787.       PREINIT:
  788.           char *host = "localhost";
  789.           CODE:
  790.           if( items > 1 )
  791.                host = (char *)SvPV(ST(1), PL_na);
  792.           RETVAL = rpcb_gettime( host, &timep );
  793.           OUTPUT:
  794.           timep
  795.           RETVAL
  796.  
  797. =head2 The ALIAS: Keyword
  798.  
  799. The ALIAS: keyword allows an XSUB to have two or more unique Perl names
  800. and to know which of those names was used when it was invoked.  The Perl
  801. names may be fully-qualified with package names.  Each alias is given an
  802. index.  The compiler will setup a variable called C<ix> which contain the
  803. index of the alias which was used.  When the XSUB is called with its
  804. declared name C<ix> will be 0.
  805.  
  806. The following example will create aliases C<FOO::gettime()> and
  807. C<BAR::getit()> for this function.
  808.  
  809.     bool_t
  810.     rpcb_gettime(host,timep)
  811.           char *host
  812.           time_t &timep
  813.       ALIAS:
  814.         FOO::gettime = 1
  815.         BAR::getit = 2
  816.       INIT:
  817.       printf("# ix = %d\n", ix );
  818.           OUTPUT:
  819.           timep
  820.  
  821. =head2 The INTERFACE: Keyword
  822.  
  823. This keyword declares the current XSUB as a keeper of the given
  824. calling signature.  If some text follows this keyword, it is
  825. considered as a list of functions which have this signature, and
  826. should be attached to XSUBs.
  827.  
  828. Say, if you have 4 functions multiply(), divide(), add(), subtract() all
  829. having the signature
  830.  
  831.     symbolic f(symbolic, symbolic);
  832.  
  833. you code them all by using XSUB
  834.  
  835.     symbolic
  836.     interface_s_ss(arg1, arg2)  
  837.     symbolic    arg1
  838.     symbolic    arg2
  839.     INTERFACE:
  840.     multiply divide 
  841.     add subtract
  842.  
  843. The advantage of this approach comparing to ALIAS: keyword is that one
  844. can attach an extra function remainder() at runtime by using
  845.     
  846.     CV *mycv = newXSproto("Symbolic::remainder", 
  847.               XS_Symbolic_interface_s_ss, __FILE__, "$$");
  848.     XSINTERFACE_FUNC_SET(mycv, remainder);
  849.  
  850. (This example supposes that there was no INTERFACE_MACRO: section,
  851. otherwise one needs to use something else instead of
  852. C<XSINTERFACE_FUNC_SET>.)
  853.  
  854. =head2 The INTERFACE_MACRO: Keyword
  855.  
  856. This keyword allows one to define an INTERFACE using a different way
  857. to extract a function pointer from an XSUB.  The text which follows
  858. this keyword should give the name of macros which would extract/set a
  859. function pointer.  The extractor macro is given return type, C<CV*>,
  860. and C<XSANY.any_dptr> for this C<CV*>.  The setter macro is given cv,
  861. and the function pointer.
  862.  
  863. The default value is C<XSINTERFACE_FUNC> and C<XSINTERFACE_FUNC_SET>.
  864. An INTERFACE keyword with an empty list of functions can be omitted if
  865. INTERFACE_MACRO keyword is used.
  866.  
  867. Suppose that in the previous example functions pointers for 
  868. multiply(), divide(), add(), subtract() are kept in a global C array
  869. C<fp[]> with offsets being C<multiply_off>, C<divide_off>, C<add_off>,
  870. C<subtract_off>.  Then one can use 
  871.  
  872.     #define XSINTERFACE_FUNC_BYOFFSET(ret,cv,f) \
  873.     ((XSINTERFACE_CVT(ret,))fp[CvXSUBANY(cv).any_i32])
  874.     #define XSINTERFACE_FUNC_BYOFFSET_set(cv,f) \
  875.     CvXSUBANY(cv).any_i32 = CAT2( f, _off )
  876.  
  877. in C section,
  878.  
  879.     symbolic
  880.     interface_s_ss(arg1, arg2)  
  881.     symbolic    arg1
  882.     symbolic    arg2
  883.     INTERFACE_MACRO: 
  884.     XSINTERFACE_FUNC_BYOFFSET
  885.     XSINTERFACE_FUNC_BYOFFSET_set
  886.     INTERFACE:
  887.     multiply divide 
  888.     add subtract
  889.  
  890. in XSUB section.
  891.  
  892. =head2 The INCLUDE: Keyword
  893.  
  894. This keyword can be used to pull other files into the XS module.  The other
  895. files may have XS code.  INCLUDE: can also be used to run a command to
  896. generate the XS code to be pulled into the module.
  897.  
  898. The file F<Rpcb1.xsh> contains our C<rpcb_gettime()> function:
  899.  
  900.     bool_t
  901.     rpcb_gettime(host,timep)
  902.           char *host
  903.           time_t &timep
  904.           OUTPUT:
  905.           timep
  906.  
  907. The XS module can use INCLUDE: to pull that file into it.
  908.  
  909.     INCLUDE: Rpcb1.xsh
  910.  
  911. If the parameters to the INCLUDE: keyword are followed by a pipe (C<|>) then
  912. the compiler will interpret the parameters as a command.
  913.  
  914.     INCLUDE: cat Rpcb1.xsh |
  915.  
  916. =head2 The CASE: Keyword
  917.  
  918. The CASE: keyword allows an XSUB to have multiple distinct parts with each
  919. part acting as a virtual XSUB.  CASE: is greedy and if it is used then all
  920. other XS keywords must be contained within a CASE:.  This means nothing may
  921. precede the first CASE: in the XSUB and anything following the last CASE: is
  922. included in that case.
  923.  
  924. A CASE: might switch via a parameter of the XSUB, via the C<ix> ALIAS:
  925. variable (see L<"The ALIAS: Keyword">), or maybe via the C<items> variable
  926. (see L<"Variable-length Parameter Lists">).  The last CASE: becomes the
  927. B<default> case if it is not associated with a conditional.  The following
  928. example shows CASE switched via C<ix> with a function C<rpcb_gettime()>
  929. having an alias C<x_gettime()>.  When the function is called as
  930. C<rpcb_gettime()> its parameters are the usual C<(char *host, time_t *timep)>,
  931. but when the function is called as C<x_gettime()> its parameters are
  932. reversed, C<(time_t *timep, char *host)>.
  933.  
  934.     long
  935.     rpcb_gettime(a,b)
  936.       CASE: ix == 1
  937.       ALIAS:
  938.       x_gettime = 1
  939.       INPUT:
  940.       # 'a' is timep, 'b' is host
  941.           char *b
  942.           time_t a = NO_INIT
  943.           CODE:
  944.                RETVAL = rpcb_gettime( b, &a );
  945.           OUTPUT:
  946.           a
  947.           RETVAL
  948.       CASE:
  949.       # 'a' is host, 'b' is timep
  950.           char *a
  951.           time_t &b = NO_INIT
  952.           OUTPUT:
  953.           b
  954.           RETVAL
  955.  
  956. That function can be called with either of the following statements.  Note
  957. the different argument lists.
  958.  
  959.     $status = rpcb_gettime( $host, $timep );
  960.  
  961.     $status = x_gettime( $timep, $host );
  962.  
  963. =head2 The & Unary Operator
  964.  
  965. The & unary operator is used to tell the compiler that it should dereference
  966. the object when it calls the C function.  This is used when a CODE: block is
  967. not used and the object is a not a pointer type (the object is an C<int> or
  968. C<long> but not a C<int*> or C<long*>).
  969.  
  970. The following XSUB will generate incorrect C code.  The xsubpp compiler will
  971. turn this into code which calls C<rpcb_gettime()> with parameters C<(char
  972. *host, time_t timep)>, but the real C<rpcb_gettime()> wants the C<timep>
  973. parameter to be of type C<time_t*> rather than C<time_t>.
  974.  
  975.     bool_t
  976.     rpcb_gettime(host,timep)
  977.           char *host
  978.           time_t timep
  979.           OUTPUT:
  980.           timep
  981.  
  982. That problem is corrected by using the C<&> operator.  The xsubpp compiler
  983. will now turn this into code which calls C<rpcb_gettime()> correctly with
  984. parameters C<(char *host, time_t *timep)>.  It does this by carrying the
  985. C<&> through, so the function call looks like C<rpcb_gettime(host, &timep)>.
  986.  
  987.     bool_t
  988.     rpcb_gettime(host,timep)
  989.           char *host
  990.           time_t &timep
  991.           OUTPUT:
  992.           timep
  993.  
  994. =head2 Inserting Comments and C Preprocessor Directives
  995.  
  996. C preprocessor directives are allowed within BOOT:, PREINIT: INIT:,
  997. CODE:, PPCODE:, and CLEANUP: blocks, as well as outside the functions.
  998. Comments are allowed anywhere after the MODULE keyword.  The compiler
  999. will pass the preprocessor directives through untouched and will remove
  1000. the commented lines.
  1001.  
  1002. Comments can be added to XSUBs by placing a C<#> as the first
  1003. non-whitespace of a line.  Care should be taken to avoid making the
  1004. comment look like a C preprocessor directive, lest it be interpreted as
  1005. such.  The simplest way to prevent this is to put whitespace in front of
  1006. the C<#>.
  1007.  
  1008. If you use preprocessor directives to choose one of two
  1009. versions of a function, use
  1010.  
  1011.     #if ... version1
  1012.     #else /* ... version2  */
  1013.     #endif
  1014.  
  1015. and not
  1016.  
  1017.     #if ... version1
  1018.     #endif
  1019.     #if ... version2
  1020.     #endif
  1021.  
  1022. because otherwise xsubpp will believe that you made a duplicate
  1023. definition of the function.  Also, put a blank line before the
  1024. #else/#endif so it will not be seen as part of the function body.
  1025.  
  1026. =head2 Using XS With C++
  1027.  
  1028. If a function is defined as a C++ method then it will assume
  1029. its first argument is an object pointer.  The object pointer
  1030. will be stored in a variable called THIS.  The object should
  1031. have been created by C++ with the new() function and should
  1032. be blessed by Perl with the sv_setref_pv() macro.  The
  1033. blessing of the object by Perl can be handled by a typemap.  An example
  1034. typemap is shown at the end of this section.
  1035.  
  1036. If the method is defined as static it will call the C++
  1037. function using the class::method() syntax.  If the method is not static
  1038. the function will be called using the THIS-E<gt>method() syntax.
  1039.  
  1040. The next examples will use the following C++ class.
  1041.  
  1042.      class color {
  1043.           public:
  1044.           color();
  1045.           ~color();
  1046.           int blue();
  1047.           void set_blue( int );
  1048.  
  1049.           private:
  1050.           int c_blue;
  1051.      };
  1052.  
  1053. The XSUBs for the blue() and set_blue() methods are defined with the class
  1054. name but the parameter for the object (THIS, or "self") is implicit and is
  1055. not listed.
  1056.  
  1057.      int
  1058.      color::blue()
  1059.  
  1060.      void
  1061.      color::set_blue( val )
  1062.           int val
  1063.  
  1064. Both functions will expect an object as the first parameter.  The xsubpp
  1065. compiler will call that object C<THIS> and will use it to call the specified
  1066. method.  So in the C++ code the blue() and set_blue() methods will be called
  1067. in the following manner.
  1068.  
  1069.      RETVAL = THIS->blue();
  1070.  
  1071.      THIS->set_blue( val );
  1072.  
  1073. If the function's name is B<DESTROY> then the C++ C<delete> function will be
  1074. called and C<THIS> will be given as its parameter.
  1075.  
  1076.      void
  1077.      color::DESTROY()
  1078.  
  1079. The C++ code will call C<delete>.
  1080.  
  1081.      delete THIS;
  1082.  
  1083. If the function's name is B<new> then the C++ C<new> function will be called
  1084. to create a dynamic C++ object.  The XSUB will expect the class name, which
  1085. will be kept in a variable called C<CLASS>, to be given as the first
  1086. argument.
  1087.  
  1088.      color *
  1089.      color::new()
  1090.  
  1091. The C++ code will call C<new>.
  1092.  
  1093.     RETVAL = new color();
  1094.  
  1095. The following is an example of a typemap that could be used for this C++
  1096. example.
  1097.  
  1098.     TYPEMAP
  1099.     color *        O_OBJECT
  1100.  
  1101.     OUTPUT
  1102.     # The Perl object is blessed into 'CLASS', which should be a
  1103.     # char* having the name of the package for the blessing.
  1104.     O_OBJECT
  1105.         sv_setref_pv( $arg, CLASS, (void*)$var );
  1106.  
  1107.     INPUT
  1108.     O_OBJECT
  1109.         if( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) )
  1110.             $var = ($type)SvIV((SV*)SvRV( $arg ));
  1111.         else{
  1112.             warn( \"${Package}::$func_name() -- $var is not a blessed SV reference\" );
  1113.             XSRETURN_UNDEF;
  1114.         }
  1115.  
  1116. =head2 Interface Strategy
  1117.  
  1118. When designing an interface between Perl and a C library a straight
  1119. translation from C to XS is often sufficient.  The interface will often be
  1120. very C-like and occasionally nonintuitive, especially when the C function
  1121. modifies one of its parameters.  In cases where the programmer wishes to
  1122. create a more Perl-like interface the following strategy may help to
  1123. identify the more critical parts of the interface.
  1124.  
  1125. Identify the C functions which modify their parameters.  The XSUBs for
  1126. these functions may be able to return lists to Perl, or may be
  1127. candidates to return undef or an empty list in case of failure.
  1128.  
  1129. Identify which values are used by only the C and XSUB functions
  1130. themselves.  If Perl does not need to access the contents of the value
  1131. then it may not be necessary to provide a translation for that value
  1132. from C to Perl.
  1133.  
  1134. Identify the pointers in the C function parameter lists and return
  1135. values.  Some pointers can be handled in XS with the & unary operator on
  1136. the variable name while others will require the use of the * operator on
  1137. the type name.  In general it is easier to work with the & operator.
  1138.  
  1139. Identify the structures used by the C functions.  In many
  1140. cases it may be helpful to use the T_PTROBJ typemap for
  1141. these structures so they can be manipulated by Perl as
  1142. blessed objects.
  1143.  
  1144. =head2 Perl Objects And C Structures
  1145.  
  1146. When dealing with C structures one should select either
  1147. B<T_PTROBJ> or B<T_PTRREF> for the XS type.  Both types are
  1148. designed to handle pointers to complex objects.  The
  1149. T_PTRREF type will allow the Perl object to be unblessed
  1150. while the T_PTROBJ type requires that the object be blessed.
  1151. By using T_PTROBJ one can achieve a form of type-checking
  1152. because the XSUB will attempt to verify that the Perl object
  1153. is of the expected type.
  1154.  
  1155. The following XS code shows the getnetconfigent() function which is used
  1156. with ONC+ TIRPC.  The getnetconfigent() function will return a pointer to a
  1157. C structure and has the C prototype shown below.  The example will
  1158. demonstrate how the C pointer will become a Perl reference.  Perl will
  1159. consider this reference to be a pointer to a blessed object and will
  1160. attempt to call a destructor for the object.  A destructor will be
  1161. provided in the XS source to free the memory used by getnetconfigent().
  1162. Destructors in XS can be created by specifying an XSUB function whose name
  1163. ends with the word B<DESTROY>.  XS destructors can be used to free memory
  1164. which may have been malloc'd by another XSUB.
  1165.  
  1166.      struct netconfig *getnetconfigent(const char *netid);
  1167.  
  1168. A C<typedef> will be created for C<struct netconfig>.  The Perl
  1169. object will be blessed in a class matching the name of the C
  1170. type, with the tag C<Ptr> appended, and the name should not
  1171. have embedded spaces if it will be a Perl package name.  The
  1172. destructor will be placed in a class corresponding to the
  1173. class of the object and the PREFIX keyword will be used to
  1174. trim the name to the word DESTROY as Perl will expect.
  1175.  
  1176.      typedef struct netconfig Netconfig;
  1177.  
  1178.      MODULE = RPC  PACKAGE = RPC
  1179.  
  1180.      Netconfig *
  1181.      getnetconfigent(netid)
  1182.           char *netid
  1183.  
  1184.      MODULE = RPC  PACKAGE = NetconfigPtr  PREFIX = rpcb_
  1185.  
  1186.      void
  1187.      rpcb_DESTROY(netconf)
  1188.           Netconfig *netconf
  1189.           CODE:
  1190.           printf("Now in NetconfigPtr::DESTROY\n");
  1191.           free( netconf );
  1192.  
  1193. This example requires the following typemap entry.  Consult the typemap
  1194. section for more information about adding new typemaps for an extension.
  1195.  
  1196.      TYPEMAP
  1197.      Netconfig *  T_PTROBJ
  1198.  
  1199. This example will be used with the following Perl statements.
  1200.  
  1201.      use RPC;
  1202.      $netconf = getnetconfigent("udp");
  1203.  
  1204. When Perl destroys the object referenced by $netconf it will send the
  1205. object to the supplied XSUB DESTROY function.  Perl cannot determine, and
  1206. does not care, that this object is a C struct and not a Perl object.  In
  1207. this sense, there is no difference between the object created by the
  1208. getnetconfigent() XSUB and an object created by a normal Perl subroutine.
  1209.  
  1210. =head2 The Typemap
  1211.  
  1212. The typemap is a collection of code fragments which are used by the B<xsubpp>
  1213. compiler to map C function parameters and values to Perl values.  The
  1214. typemap file may consist of three sections labeled C<TYPEMAP>, C<INPUT>, and
  1215. C<OUTPUT>.  The INPUT section tells the compiler how to translate Perl values
  1216. into variables of certain C types.  The OUTPUT section tells the compiler
  1217. how to translate the values from certain C types into values Perl can
  1218. understand.  The TYPEMAP section tells the compiler which of the INPUT and
  1219. OUTPUT code fragments should be used to map a given C type to a Perl value.
  1220. Each of the sections of the typemap must be preceded by one of the TYPEMAP,
  1221. INPUT, or OUTPUT keywords.
  1222.  
  1223. The default typemap in the C<ext> directory of the Perl source contains many
  1224. useful types which can be used by Perl extensions.  Some extensions define
  1225. additional typemaps which they keep in their own directory.  These
  1226. additional typemaps may reference INPUT and OUTPUT maps in the main
  1227. typemap.  The B<xsubpp> compiler will allow the extension's own typemap to
  1228. override any mappings which are in the default typemap.
  1229.  
  1230. Most extensions which require a custom typemap will need only the TYPEMAP
  1231. section of the typemap file.  The custom typemap used in the
  1232. getnetconfigent() example shown earlier demonstrates what may be the typical
  1233. use of extension typemaps.  That typemap is used to equate a C structure
  1234. with the T_PTROBJ typemap.  The typemap used by getnetconfigent() is shown
  1235. here.  Note that the C type is separated from the XS type with a tab and
  1236. that the C unary operator C<*> is considered to be a part of the C type name.
  1237.  
  1238.      TYPEMAP
  1239.      Netconfig *<tab>T_PTROBJ
  1240.  
  1241. Here's a more complicated example: suppose that you wanted C<struct
  1242. netconfig> to be blessed into the class C<Net::Config>.  One way to do
  1243. this is to use underscores (_) to separate package names, as follows:
  1244.  
  1245.         typedef struct netconfig * Net_Config;
  1246.  
  1247. And then provide a typemap entry C<T_PTROBJ_SPECIAL> that maps underscores to
  1248. double-colons (::), and declare C<Net_Config> to be of that type:
  1249.  
  1250.  
  1251.         TYPEMAP
  1252.         Net_Config      T_PTROBJ_SPECIAL
  1253.  
  1254.         INPUT
  1255.         T_PTROBJ_SPECIAL
  1256.                 if (sv_derived_from($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")) {
  1257.                         IV tmp = SvIV((SV*)SvRV($arg));
  1258.                 $var = ($type) tmp;
  1259.                 }
  1260.                 else
  1261.                         croak(\"$var is not of type ${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")
  1262.  
  1263.         OUTPUT
  1264.         T_PTROBJ_SPECIAL
  1265.                 sv_setref_pv($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\",
  1266.                 (void*)$var);
  1267.  
  1268. The INPUT and OUTPUT sections substitute underscores for double-colons
  1269. on the fly, giving the desired effect.  This example demonstrates some
  1270. of the power and versatility of the typemap facility.
  1271.  
  1272. =head1 EXAMPLES
  1273.  
  1274. File C<RPC.xs>: Interface to some ONC+ RPC bind library functions.
  1275.  
  1276.      #include "EXTERN.h"
  1277.      #include "perl.h"
  1278.      #include "XSUB.h"
  1279.  
  1280.      #include <rpc/rpc.h>
  1281.  
  1282.      typedef struct netconfig Netconfig;
  1283.  
  1284.      MODULE = RPC  PACKAGE = RPC
  1285.  
  1286.      SV *
  1287.      rpcb_gettime(host="localhost")
  1288.           char *host
  1289.       PREINIT:
  1290.           time_t  timep;
  1291.           CODE:
  1292.           ST(0) = sv_newmortal();
  1293.           if( rpcb_gettime( host, &timep ) )
  1294.                sv_setnv( ST(0), (double)timep );
  1295.  
  1296.      Netconfig *
  1297.      getnetconfigent(netid="udp")
  1298.           char *netid
  1299.  
  1300.      MODULE = RPC  PACKAGE = NetconfigPtr  PREFIX = rpcb_
  1301.  
  1302.      void
  1303.      rpcb_DESTROY(netconf)
  1304.           Netconfig *netconf
  1305.           CODE:
  1306.           printf("NetconfigPtr::DESTROY\n");
  1307.           free( netconf );
  1308.  
  1309. File C<typemap>: Custom typemap for RPC.xs.
  1310.  
  1311.      TYPEMAP
  1312.      Netconfig *  T_PTROBJ
  1313.  
  1314. File C<RPC.pm>: Perl module for the RPC extension.
  1315.  
  1316.      package RPC;
  1317.  
  1318.      require Exporter;
  1319.      require DynaLoader;
  1320.      @ISA = qw(Exporter DynaLoader);
  1321.      @EXPORT = qw(rpcb_gettime getnetconfigent);
  1322.  
  1323.      bootstrap RPC;
  1324.      1;
  1325.  
  1326. File C<rpctest.pl>: Perl test program for the RPC extension.
  1327.  
  1328.      use RPC;
  1329.  
  1330.      $netconf = getnetconfigent();
  1331.      $a = rpcb_gettime();
  1332.      print "time = $a\n";
  1333.      print "netconf = $netconf\n";
  1334.  
  1335.      $netconf = getnetconfigent("tcp");
  1336.      $a = rpcb_gettime("poplar");
  1337.      print "time = $a\n";
  1338.      print "netconf = $netconf\n";
  1339.  
  1340.  
  1341. =head1 XS VERSION
  1342.  
  1343. This document covers features supported by C<xsubpp> 1.935.
  1344.  
  1345. =head1 AUTHOR
  1346.  
  1347. Dean Roehrich <F<roehrich@cray.com>>
  1348. Jul 8, 1996
  1349.